package homelesspartners.client.widgets;

import homelesspartners.client.model.City;
import homelesspartners.client.model.Shelter;
import homelesspartners.client.model.User;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class GiftTabWidget extends Composite {

	private DockPanel dockPanel = new DockPanel();

	private GiftTabMenuWidget giftTabMenu = new GiftTabMenuWidget();

	private Widget currentContent = null;

	private static CitiesWidget citiesWidget = CitiesWidget.getInstance();
	private static SheltersWidget sheltersWidget = SheltersWidget.getInstance();
	private static StoriesWidget storiesWidget = StoriesWidget.getInstance();
	private static ShoppingListWidget shoppingListWidget = ShoppingListWidget.getInstance();
	private static FinishShoppingWidget finishShoppingWidget = FinishShoppingWidget.getInstance();

	private static GiftTabWidget instance = new GiftTabWidget();

	public static GiftTabWidget getInstance() {
		return instance;
	}

	public GiftTabWidget() {

		setHeader(giftTabMenu);
		Image image = new Image("./images/fader.png");
		image.setWidth("728px");
		setHeader(image);
		setContent(citiesWidget);

		VerticalPanel sidePanel = new VerticalPanel();
		Image image2 = new Image("./images/remove.jpg"); // GWT bug, seems to
															// need push button
															// images loaded
															// else where first,
															// even if invisible
		sidePanel.add(image2);
		image2.setVisible(false);
		sidePanel.add(shoppingListWidget);
		sidePanel.add(SponsorsWidget.getInstance());
		setSidePanel(sidePanel);

		initWidget(dockPanel);

	}

	public void setSidePanel(Widget sideWidget) {
		dockPanel.add(sideWidget, DockPanel.EAST);
	}

	public void setHeader(Widget headerWidget) {
		dockPanel.add(headerWidget, DockPanel.NORTH);
	}

	public void setFooter(Widget footerWidget) {
		dockPanel.add(footerWidget, DockPanel.SOUTH);
	}

	public void setContent(Widget contentWidget) {
		if (currentContent != null) {
			dockPanel.remove(currentContent);
		}
		contentWidget.setWidth("520px");
		currentContent = contentWidget;
		dockPanel.add(contentWidget, DockPanel.CENTER);
	}

	private String appendValue(String original, String valueToAppend) {
		if (null == original) {
			return valueToAppend;
		}
		String startOfAppendSubstring = " (";
		int indexOfPreviousAppend = original.indexOf(startOfAppendSubstring);
		if (indexOfPreviousAppend >= 0) {
			original = original.substring(0, indexOfPreviousAppend);
		}
		return original + startOfAppendSubstring + valueToAppend + ")";
	}

	public void showChooseCity() {
		setContent(citiesWidget);
		giftTabMenu.showChooseCity();
		shoppingListWidget.setVisible(true);
		SponsorsWidget.getInstance().setVisible(true);
	}

	public void showChooseShelter() {
		City city = User.getInstance().getCurrentCity();
		if (city == null) {
			showChooseCity();
			return;
		}
		sheltersWidget.refresh(city);
		setContent(sheltersWidget);
		giftTabMenu.showChooseShelter();
		shoppingListWidget.setVisible(true);
		SponsorsWidget.getInstance().setVisible(true);
	}

	public void showStories() {
		Shelter shelter = User.getInstance().getCurrentShelter();
		if (shelter == null) {
			showChooseShelter();
			return;
		}
		storiesWidget.refresh(shelter);
		setContent(storiesWidget);
		giftTabMenu.showStories();
		shoppingListWidget.setVisible(true);
		SponsorsWidget.getInstance().setVisible(true);
	}

	public void showFinishShopping() {
		setContent(finishShoppingWidget);
		shoppingListWidget.setVisible(false);
		SponsorsWidget.getInstance().setVisible(false);
		giftTabMenu.showFinish();
		finishShoppingWidget.refresh();
	}

}
